<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\home\controller\member;

use think\Db;
use service\DataService;
use service\ToolsService;
use service\LogService;

/**
 * Home后端-传承人
 * Class Client
 * @package app\home\controller\member
 * @author pp <124910168@qq.com>
 * @date 2020/06/04 10:41
 */
class Client extends Admin
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $map[$key] = ['eq', $get[$key]];
                }else{
                    $map['username|truename|honor|advantage|phone|id_no|cert_no|desc|content'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->client->getPageLists($map, '', '');
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['honors'] = $list[0][$k]['honor'] ? explode("\r\n", $v['honor']) : [];
                $list[0][$k]['advantages'] = $list[0][$k]['advantage'] ? explode("\r\n", $v['advantage']) : [];
            }
        }
        // halt($list);

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'kw' => !empty($get['kw']) ? $get['kw'] : '',
            'aid' => !empty($get['aid']) ? $get['aid'] : '',
            'nid' => !empty($get['nid']) ? $get['nid'] : '',
            'did' => !empty($get['did']) ? $get['did'] : '',
        ];
        return $this->fetch('', $data);
    }
        
    public function detail(){
        $lMap = ['catid' => 7, 'status' => 1, 'id' => 54];
        $info = $this->article->where($lMap)->find();
        // halt($info);

        $data = [
            'info' => $info,
        ];
        return $this->fetch('', $data);
    }
        
    public function apply(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // halt($data);

            if(!empty($data['work_start'])){
                $data['work_start']=strtotime($data['work_start']);
            }

            if(!empty($data['id_images'])){
                $data['id_images']=trim(implode(';',$data['id_images']),',');
            }

            if(!empty($data['cert_images'])){
                $data['cert_images']=trim(implode(';',$data['cert_images']),',');
            }

            if (isset($data['authorize']) && is_array($data['authorize'])) {
                $data['authorize'] = join(',', $data['authorize']);
            }
            
            //商家分类  用,分隔
            if(!empty($data['sc_id'])){
            	$data['sc_id']=trim(implode(',',$data['sc_id']),',');
            }
            if(!empty($data['type'])){
                $data['type']=trim(implode(',',$data['type']),',');
            }
            if(!empty($data['readgroup'])){
                $data['readgroup']=trim(implode(',',$data['readgroup']),',');
            }
            
            if (isset($data['id'])) {
                unset($data['username']);
                $up = $this->client->update($data);
                if($up){
                    $this->success('信息更新成功');
                }
            // } elseif (Db::name($this->table)->where('username', $data['username'])->find()) {
            //     $this->error('用户账号已经存在，请使用其它账号！');
            // }
            }else{
                $data['admin_id'] = $this->uid;
                $data['readgroup'] = 1;
                $in = $this->client->insert($data);
                if($in){
                    $this->success('信息提交成功，等待管理员审核');
                }
            }
        } else {
            $data = $this->client->getOneDarry(['admin_id' => $this->uid]);

            if(!empty($data['work_start'])){
                $data['work_start']=date('Y-m-d',$data['work_start']);
            }
            if(!empty($data['id_images'])){
                $data['id_images']=explode(';',$data['id_images']);
            }
            if(!empty($data['cert_images'])){
                $data['cert_images']=explode(';',$data['cert_images']);
            }
            if(!empty($data['readgroup'])){
                $data['readgroup']=explode(',',$data['readgroup']);
            }
            if(!empty($data['aid'])){
                $data['aid']=explode(',',$data['aid']);
            }
            if(!empty($data['nid'])){
                $data['nid']=explode(',',$data['nid']);
            }
            if(!empty($data['did'])){
                $data['did']=explode(',',$data['did']);
            }

            // //获取地区
            // $areaList = $this->_getAreaTrees('Area', ['area_open' => 1, 'area_id' => ['in', $this->aids]]);
            // $this->assign('areaList', $areaList);
            // $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
            // $this->assign('authorizes', Db::name('ClientAuth')->where('create_by',0)->select());

            // $newareas = ToolsService::arr2subs($this->newareas, $data['aid']);
            // $this->assign('newareas', $newareas);
            // $newnations = ToolsService::arr2subss($this->newnations, $data['nid']);
            // $this->assign('newnations', $newnations);
            // $newdiseases = ToolsService::arr2subss($this->newdiseases, $data['did']);
            // $this->assign('newdiseases', $newdiseases);

            // halt($data);
            $data = [
                'vo' => $data,
            ];
            return $this->fetch('', $data);
        }
    }
}
